package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    private final ClazzServiceImpl clazzServiceImpl;
    private final StudentMapper studentMapper;

    public StudentServiceImpl(ClazzServiceImpl clazzServiceImpl, StudentMapper studentMapper) {
        this.clazzServiceImpl = clazzServiceImpl;
        this.studentMapper = studentMapper;
    }

    @Override
    public PageResult<Student> getPage(StudentQueryParam param) {

        // 设置分页请求参数查询
        Page<Student> page = Page.of(param.getPage(), param.getPageSize());
        page.addOrder(OrderItem.desc("update_time"));
        //条件分页查询
       page =  lambdaQuery().like(param.getName() != null && !param.getName().isEmpty(), Student::getName, param.getName())
                .eq(param.getDegree() != null, Student::getDegree, param.getDegree())
                .eq(param.getClazzId() != null, Student::getClazzId, param.getClazzId())
                .page(page);
        List<Integer> clazzIds = page.getRecords().stream().map(student -> student.getClazzId()).collect(Collectors.toList());
        if (clazzIds.isEmpty()){
            return new PageResult<>(page.getTotal(), page.getRecords());
        }
        List<Clazz> clazzList = clazzServiceImpl.listByIds(clazzIds);
        Map<Integer, String> clazzMap = clazzList.stream().collect(Collectors.toMap(Clazz::getId, Clazz::getName));
        for (Student student : page.getRecords()) {
           if(clazzMap.containsKey(student.getClazzId())){
               student.setClazzName(clazzMap.get(student.getClazzId()));
           }
        }
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    @Override
    public void saveStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        save(student);
    }

    @Override
    public void updateStudentById(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        updateById(student);
    }

    @Override
    public void violation(Integer id, Integer score) {
        Student byId = getById(id);
        // 更新学生违纪信息
        if(byId != null){
            byId.setUpdateTime(LocalDateTime.now());
            byId.setViolationCount( (short) (byId.getViolationCount() + 1));
            byId.setViolationScore((short)(byId.getViolationScore() + score));
        }

        updateById(byId);
    }
}
